package com.the4man.cnbdw.action;

import com.opensymphony.xwork2.ModelDriven;
import com.the4man.cnbdw.service.FenceInfoService;
import com.the4man.cnbdw.service.LogInfoService;
import com.the4man.cnbdw.service.SmapInfoService;
import com.the4man.cnbdw.utils.Page;
import com.the4man.framework.action.BaseAction;
import com.the4man.framework.model.*;
import com.the4man.framework.model.gen.FenceInfoExampleBase;
import com.the4man.framework.util.DateUtil;
import com.the4man.framework.util.LOG;
import com.the4man.framework.util.PageJson;
import com.the4man.framework.utils.RequestUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Action(
        value = "fenceInfoAction",
        results = {@Result(
                name = "addPage",
                location = "/cnbdw/fenceInfo/add.jsp"
        ), @Result(
                name = "listPage",
                location = "/cnbdw/fenceInfo/list.jsp"
        ), @Result(
                name = "editPage",
                location = "/cnbdw/fenceInfo/edit.jsp"
        )}
)
public class FenceInfoAction extends BaseAction implements ModelDriven<FenceInfo> {
    private FenceInfo fenceInfo = new FenceInfo();
    private LogInfo logInfo = new LogInfo();
    @Autowired
    private LogInfoService logService;
    @Autowired
    private FenceInfoService fenceInfoService;
    @Autowired
    private SmapInfoService smapInfoService;

    public FenceInfoAction() {
    }

    public String indexPage() {
        return "listPage";
    }

    public String toAddPage() {
        try {
            String smapId = this.getRequest().getParameter("smapId");
            Object instance = this.smapInfoService.selectByPrimaryKey(smapId, SmapInfo.class);
            this.getRequest().setAttribute("smap", instance);
            this.getRequest().setAttribute("smapId", smapId);
        } catch (Exception var3) {
            LOG.error("获取数据失败", var3, new String[0]);
        }

        return "addPage";
    }

    public String toEditPage() {
        try {
            Object instance = this.fenceInfoService.selectByPrimaryKey(this.fenceInfo.getId(), FenceInfo.class);
            BeanUtils.copyProperties(instance, this.getModel());
            FenceInfo fence = (FenceInfo)instance;
            Object smap = this.smapInfoService.selectByPrimaryKey(fence.getSmapId(), SmapInfo.class);
            this.getRequest().setAttribute("smap", smap);
        } catch (Exception var4) {
            LOG.error("获取数据失败", var4, new String[0]);
        }

        return "editPage";
    }

    public void getAllDatasourceList() {
        try {
            FenceInfoExample fenceInfoExample = new FenceInfoExample();
            FenceInfoExampleBase.Criteria criteria = fenceInfoExample.createCriteria();
            String name = this.getRequest().getParameter("name");
            if(StringUtils.isNotBlank(name)) {
                criteria.andNameLike("%" + name + "%");
                fenceInfoExample.or().andCodeLike("%" + name + "%");
            }

            Page<FenceInfo> page = new Page();
            this.fenceInfoService.selectByExample(fenceInfoExample, page);
            DataGrid d = new DataGrid();
            d.setRows(page.getData());
            d.setTotal(Long.valueOf((long)page.getRowCount()));
            this.writeJson(d);
        } catch (Exception var6) {
            LOG.error("获取数据失败", var6, new String[0]);
        }

    }

    public void add() {
        PageJson json = new PageJson();

        try {
            FenceInfoExample fenceInfoExample = new FenceInfoExample();
            fenceInfoExample.createCriteria().andCodeEqualTo(this.fenceInfo.getCode());
            List list = this.fenceInfoService.selectByExample(fenceInfoExample);
            if(list != null && !list.isEmpty()) {
                json.setReturnFlag(false);
                json.setReturnMsg("存在相同mac的ap");
                this.writeJson(json);
                return;
            }

            this.fenceInfo.setId(UUID.randomUUID().toString());
            String time = DateUtil.getInstance().formatDate(new Date());
            this.fenceInfo.setCreateTime(time);
            this.fenceInfo.setUpdateTime(time);
            this.fenceInfo.setCreateUser(RequestUtil.getSessionUserinfo().getUsername());
            this.fenceInfo.setUpdateUser(RequestUtil.getSessionUserinfo().getUsername());
            this.fenceInfoService.insert(this.fenceInfo);
            this.logInfo.setId(UUID.randomUUID().toString());
            this.logInfo.setTime(DateUtil.getInstance().formatDate(new Date()));
            this.logInfo.setUserName(RequestUtil.getSessionUserinfo().getUsername());
            this.logInfo.setUserId(RequestUtil.getSessionUserinfo().getUserid());
            this.logInfo.setFunc("报警区域");
            this.logInfo.setOp("报警区域添加");
            this.logInfo.setMemo("报警区域名:" + this.fenceInfo.getName());
            this.logService.insert(this.logInfo);
            json.setReturnFlag(true);
            json.setReturnMsg("提交成功！");
        } catch (Exception var5) {
            json.setReturnFlag(false);
            json.setReturnMsg("提交出错了");
            LOG.error("获取数据失败", var5, new String[0]);
        }

        this.writeJson(json);
    }

    public void update() {
        PageJson json = new PageJson();

        try {
            FenceInfoExample fenceInfoExample = new FenceInfoExample();
            fenceInfoExample.createCriteria().andCodeEqualTo(this.fenceInfo.getCode()).andIdNotEqualTo(this.fenceInfo.getId());
            List<FenceInfo> list = this.fenceInfoService.selectByExample(fenceInfoExample);
            if(list != null && !list.isEmpty()) {
                json.setReturnFlag(false);
                json.setReturnMsg("存在相同mac的ap");
                this.writeJson(json);
                return;
            }

            this.fenceInfo.setUpdateTime(DateUtil.getInstance().formatDate(new Date()));
            this.fenceInfo.setUpdateUser(RequestUtil.getSessionUserinfo().getUsername());
            this.fenceInfoService.updateByPrimaryKey(this.fenceInfo);
            this.logInfo.setId(UUID.randomUUID().toString());
            this.logInfo.setTime(DateUtil.getInstance().formatDate(new Date()));
            this.logInfo.setUserName(RequestUtil.getSessionUserinfo().getUsername());
            this.logInfo.setUserId(RequestUtil.getSessionUserinfo().getUserid());
            this.logInfo.setFunc("报警区域");
            this.logInfo.setOp("报警区域修改");
            this.logInfo.setMemo("报警区域名:" + this.fenceInfo.getName());
            this.logService.insert(this.logInfo);
            json.setReturnFlag(true);
            json.setReturnMsg("提交成功！");
        } catch (Exception var4) {
            json.setReturnFlag(false);
            json.setReturnMsg("提交出错了");
            LOG.error("获取数据失败", var4, new String[0]);
        }

        this.writeJson(json);
    }

    public String executeModifyResult() {
        try {
            Object instance = this.fenceInfoService.selectByPrimaryKey(this.fenceInfo.getId(), FenceInfo.class);
            this.getRequest().setAttribute("instance", instance);
        } catch (Exception var2) {
            LOG.error("获取数据失败", var2, new String[0]);
        }

        return "modifyPage";
    }

    public void delete() {
        PageJson json = new PageJson();

        try {
            int i = this.fenceInfoService.deleteByPrimaryKey(this.getRequest().getParameter("ids").toString(), FenceInfo.class);
            if(i > 0) {
                this.logInfo.setId(UUID.randomUUID().toString());
                this.logInfo.setTime(DateUtil.getInstance().formatDate(new Date()));
                this.logInfo.setUserName(RequestUtil.getSessionUserinfo().getUsername());
                this.logInfo.setUserId(RequestUtil.getSessionUserinfo().getUserid());
                this.logInfo.setFunc("报警区域");
                this.logInfo.setOp("报警区域添加");
                this.logInfo.setMemo("报警区域名:" + this.fenceInfo.getName());
                this.logService.insert(this.logInfo);
                json.setReturnMsg("删除报警区域成功！");
                json.setReturnFlag(true);
            } else {
                json.setReturnMsg("删除报警区域失败！");
                json.setReturnFlag(false);
            }
        } catch (Exception var3) {
            LOG.error("批量删除报警区域失败！", var3, new String[0]);
            json.setReturnMsg("删除报警区域失败！");
            json.setReturnFlag(false);
        }

        this.writeJson(json);
    }

    public void verify() {
        PageJson json = new PageJson();
        this.writeJson(json);
    }

    public FenceInfo getModel() {
        return this.fenceInfo;
    }
}
